package com.towa.ninemanmorris;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

public class Tasker extends AsyncTask<String, Void, Void> {

	private Socket socket;
	private BufferedReader br;
	private PrintWriter pw;
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}
	
	@Override
	protected Void doInBackground(String... params) {
		if (socket == null) {
			try {
				Log.i("Tasker", "creating socket");
				socket = new Socket(NineManMorrisConstants.INET_ADDRESS, NineManMorrisConstants.INET_PORT);
				Log.i("Tasker", "socket created");
			} catch (UnknownHostException e) {
				Log.e("C2DM", e.toString());
			} catch (IOException e) {
				Log.e("C2DM", e.toString());
			}
		}
		try {
//			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//			string = br.readLine();
			Log.i("Tasker", "begin printing " + params[0]);
			pw = new PrintWriter(socket.getOutputStream());
			pw.println(params[0]);
			pw.flush();
			Log.i("Tasker", "printing done");
		} catch (IOException e) {
			Log.e("C2DM", e.toString());
		}
		return null;
	}

}
